package r25.engine;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Composition {
	private String name;
	private String description;
	private Map<String, Integer> composition = new HashMap<String, Integer>();

	public Composition(String name, String description,
			Map<String, Integer> comps) {
		this.name = name;
		this.description = description;
		this.composition.putAll(comps);
	}

	@SuppressWarnings("unchecked")
	public Composition(String name, Map<String, Object> value) {
		this(name, (String) value.get("description"),
				(Map<String, Integer>) value.get("composition"));
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Map<String, Integer> getComposition() {
		return composition;
	}

	public void setComposition(Map<String, Integer> composition) {
		this.composition = composition;
	}

	public int countCompositionSize() {
		int rc = 0;
		for (Integer i : composition.values()) {
			rc += i;
		}
		return rc;
	}

	@Override
	public String toString() {
		StringBuffer rc = new StringBuffer();
		rc.append("[ ");
		rc.append(name);
		rc.append(":");
		rc.append(description);
		rc.append(" #");
		rc.append(countCompositionSize());
		for (Entry<String, Integer> e : composition.entrySet()) {
			rc.append(" ");
			rc.append(e.getKey());
			rc.append("#");
			rc.append(e.getValue());
		}
		rc.append(" ]");
		return rc.toString();
	}
}
